<?php

//-----------------------------------------------------------------------------

function f_control($reroute) {

    if ($reroute == 'from_get') {
        $action = 'home';
        if (isset($_GET['action'])) {       // isset($_SESSION['state']
            $action = $_GET['action'];
        } else {
            f_init();
        }
    } else {
        $action = $reroute;
    }

    switch($action) {
        case 'home':
            f_view_home();
            break;
        case 'new':
            f_control_new();
            break;
        case 'browse':
            f_view_browse();
            break;
        case 'login':
            f_control_login();
            $action = 'home';
            f_view_home();
            break;
        case 'register':
            f_control_register();
            break;
        default:
            f_error('ACTION UNKNOWN');
            break;
    }
}

//-----------------------------------------------------------------------------

function f_control_new() {
    if(isset($_GET['page'])) {
        $_SESSION['page'] = f_get_page_from_code($_GET['page']);
    } else if (isset($_GET['submit'])) {
        //if ($_GET['submit'] != count($_SESSION['codes'])) {
            $_SESSION['page'] = f_get_page_from_code($_GET['submit']+1);
            foreach ($_POST as $posted => $value) {
                if (isset($_SESSION['patron'][$posted])) {
                    $_SESSION['patron'][$posted] = $value;
                }
            }
        //}
        if (isset($_POST['valid'])) {
            $req = 'INSERT INTO patterns VALUES ( NULL, ';
            $first = True;
            foreach($_SESSION['patron'] as $value) {
                if(!$first) {
                    $req = $req . ",";
                } else {
                    $first = False;
                }
                $req = $req . "'" . $value . "'";
            }
            $req = $req . ")";
            f_connect();
            $result = mysql_query($req);
            if(!$result) {
                echo $req;
                f_error("INSERT NEW PATTERN FAILED");
            }
            f_control('home');
            return;
        }
    }
    f_view_new();
}

//-----------------------------------------------------------------------------

function f_control_register() {
    f_connect();

    if (!isset($_POST['valid'])) { // POST !
        f_view_register();
        return;
    }
    
    // Already exist?
    $exist = False;
    $result = mysql_query("SELECT login FROM users");
    if (!$result) {
        f_error('SELECT USERS');
    }
    while ($user = mysql_fetch_array($result)) {
        if ($user[0] == $_POST['login']) {
            $exist = True;
            break;
        }
    }
    if ($exist) {
        f_error("USER ALREADY EXIST");
        die("arg");
    }

    $result = mysql_query("INSERT INTO users (login, password,name,organization,email) VALUES ('" . $_POST['login'] . "','" . $_POST['password'] . "','" . $_POST['name'] . "','" . $_POST['organisation'] . "','" . $_POST['email'] ."')");
    if (!$result) {
        f_error("USER CREATION FAILED");
        die("arg");
    } else { // OK
        $_SESSION['user']['login'] = $_POST['login'];
        $_SESSION['user']['name'] = $_POST['name'];
        $_SESSION['user']['organisation'] = $_POST['organisation'];
        $_SESSION['user']['email'] = $_POST['email'];
        f_control('home');
    }
}

//-----------------------------------------------------------------------------

function f_control_login() {
    f_connect();

        $result = mysql_query("SELECT login, password FROM users");
        $valid = False;
        while ($user = mysql_fetch_array($result)) {
            if ($user[0] == $_POST['login'] and $user[1] == $_POST['password']) {
                $valid = True;
                break;
            }
        }
        if (!$valid) {
            f_error("INVALID USER");
            die("arg");
        } else { // OK
            $_SESSION['user']['login'] = $_POST['login'];
        }
}

?>

